package com.xored.glance.internal.core;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Tree;

import com.xored.glance.core.ITextContainer;
import com.xored.glance.internal.st.StyledTextControl;
import com.xored.glance.internal.tables.TableControl;
import com.xored.glance.internal.trees.TreeControl;

public class GlanceHandler extends AbstractHandler {

	protected Rectangle getBounds(Control control) {
		Monitor[] monitors = control.getDisplay().getMonitors();
		Rectangle total = control.getDisplay().getBounds();
		Rectangle bounds = control.getBounds();
		Point point = control.toDisplay(bounds.x, bounds.y);
		int x1, y1, x2, y2, x3, y3, x4, y4;
		// top left
		Rectangle rect = getBounds(point.x, point.y, monitors);
		if (rect != null) {
			x1 = rect.x;
			y1 = rect.y;
		} else {
			x1 = total.x;
			y1 = total.y;
		}
		// top right
		rect = getBounds(point.x + bounds.width, point.y, monitors);
		if (rect != null) {
			x2 = rect.x + rect.width;
			y2 = rect.y;
		} else {
			x2 = total.x + total.width;
			y2 = total.y;
		}
		// bottom left
		rect = getBounds(point.x, point.y + bounds.height, monitors);
		if (rect != null) {
			x3 = rect.x;
			y3 = rect.y + rect.height;
		} else {
			x3 = total.x;
			y3 = total.y + total.height;
		}
		// bottom left
		rect = getBounds(point.x + bounds.width, point.y + bounds.height,
				monitors);
		if (rect != null) {
			x4 = rect.x + rect.width;
			y4 = rect.y + rect.height;
		} else {
			x4 = total.x + total.height;
			y4 = total.y + total.height;
		}
		int x = Math.min(x1, x3);
		int y = Math.min(y1, y2);
		int xx = Math.max(x2, x4);
		int yy = Math.max(y3, y4);
		return new Rectangle(x, y, xx - x, yy - y);
	}

	protected Rectangle getBounds(int x, int y, Monitor[] monitors) {
		for (Monitor monitor : monitors) {
			Rectangle rectangle = monitor.getBounds();
			if (rectangle.contains(x, y))
				return rectangle;
		}
		return null;
	}

	public Object execute(ExecutionEvent event) throws ExecutionException {
		ITextContainer textControl = null;
		Control control = Display.getCurrent().getFocusControl();
		if (control instanceof Table) {
			textControl = new TableControl((Table) control);
		} else if (control instanceof Tree) {
			textControl = new TreeControl((Tree) control);
		} else if (control instanceof StyledText) {
			textControl = new StyledTextControl((StyledText) control);
		}
		if (textControl != null) {
			Display display = Display.getCurrent();
			PopupSearchDialog dialog = new PopupSearchDialog(
					display.getActiveShell(), textControl);
			dialog.open();
			dialog.getShell().setSize(300, 200);
		}
		return null;
	}
}
